use std::fs;
use std::io::copy;
use std::path::Path;
use zip;
#[tauri::command]
pub fn unzip_uri(path: String, target: String) -> Result<bool, String> {
    let file_path = Path::new(&path);
    let target_path = Path::new(&target);
    let zip_file = fs::File::open(&file_path).unwrap();
    let mut archive = zip::ZipArchive::new(zip_file).unwrap();
    if !target_path.exists() {
        fs::create_dir_all(target_path).map_err(|err| err.to_string())?;
    }
    for i in 0..archive.len() {
        let mut file = archive.by_index(i).unwrap();
        if file.is_dir() {
            let tmp_target = target_path.join(Path::new(&file.name().replace("\\", "")));
            fs::create_dir_all(tmp_target.as_path()).map_err(|err| err.to_string())?;
        } else {
            let tmp_file_path = target_path.join(Path::new(&file.name()));
            if !tmp_file_path.exists() {
                let mut f = fs::File::create(tmp_file_path).unwrap();
                copy(&mut file, &mut f).expect("Cannot create file and extract.");
            } else {
                let mut f = fs::File::open(tmp_file_path).unwrap();
                copy(&mut file, &mut f).expect("Cannot open file and extract.");
            }
        }
    }
    Ok(true)
}
